/*#########################################
# Copyright (c) [2010-2011], Josef Robert Novak
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
#  modification, are permitted #provided that the following conditions
#  are met:
#
# * Redistributions of source code must retain the above copyright 
#    notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above 
#    copyright notice, this list of #conditions and the following 
#    disclaimer in the documentation and/or other materials provided 
#    with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
# OF THE POSSIBILITY OF SUCH DAMAGE.
#########################################*/
#include <getopt.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include "htk2wfst.h"
using namespace std;

int main( int argc , char **argv ) {
    
  int c; int htk_flag=0; int prefix_flag=0;
  /** Defaults, lots of defaults **/
  const char* htk_models;
  const char* prefix;
  char help_info[2048];
  sprintf( help_info, "\
Syntax:  htk-am2wfst -t hmmdefs [-p prefix] \n\
Parameters:\n\
 Files and models*:\n\
   -t --htk:    HTK hmmdefs models file. Must be in text format.\n\
   -p --prefix: Prefix for output files and symbol tables.  Defaults to 'prefix'.\n\
   -h --help:   Print this help message.\n\n\
Summary: Transforms an HTK-format acoustic model to the intermediate text-format \n\
         described by the AT&T amintro(1) utility.  The input hmmdefs file is split \n\
         into several components: \n\
            PREFIX.hmm:  hmms and associated states \n\
            PREFIX.st:   states and associated mixture weights \n\
            PREFIX.var:  variance data \n\
            PREFIX.mean: means data \n\
            PREFIX.pdf:  links the mean and variance information \n\
            PREFIX.dur:  duration information \n\
         'PREFIX' may be specified by the user. \n\
         Symbol tables for the components are also generated as needed.\n\n", argv[0], argv[0] );
    
  /* Begin argument parsing */
  while( 1 ) {
    static struct option long_options[] = 
      {
	/* These options set a flag */
	{"htk",      required_argument, 0, 't'},
	{"prefix",   required_argument, 0, 'p'},  
	{"help",     no_argument, 0, 'h'},
	{0,0,0,0}
      };        
    int option_index = 0;
    c = getopt_long( argc, argv, "ht:p:", long_options, &option_index );
    if ( c == -1 )
      break;
    switch ( c ) {
       case   0: if ( long_options[option_index].flag != 0 )   break;
       case 't': htk_flag = 1; htk_models = optarg; break;
       case 'p': prefix_flag = 1; prefix = optarg; break;
       case 'h': printf("%s", help_info); exit(0); break;
       default: abort();
    }
  }
    
  if( htk_flag==0 || htk_models=="" ){
    printf("%s", help_info);
    exit(0);
  }

  if( prefix_flag==0 || prefix=="" )
    prefix = "prefix";

  HTK2WFST *acoustic_model = new HTK2WFST( prefix ) ;
  cout << "Reading in hmmdefs file and transforming to AT&T text format..." << endl;
  acoustic_model->parse_hmmdefs( htk_models ) ;
  delete acoustic_model;
  return 0 ;
}





